package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* HexagramView
* */
class HexagramView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

//    private val mPaint by lazy {
//        Paint().apply {
//            isAntiAlias = true
//            isDither = true
//            //strokeCap = Paint.Cap.ROUND
//            style = Paint.Style.STROKE
//            color = context.resources.getColor(R.color.color_AEEA00)
//        }
//    }
//
//    private val mPaint0 by lazy {
//        Paint().apply {
//            isAntiAlias = true
//            isDither = true
//            //strokeCap = Paint.Cap.ROUND
//            style = Paint.Style.FILL
//            color = context.resources.getColor(R.color.color_FF3D00)
//        }
//    }
//
//    private val mPaint1 by lazy {
//        Paint().apply {
//            isAntiAlias = true
//            isDither = true
//            //strokeCap = Paint.Cap.ROUND
//            style = Paint.Style.STROKE
//            color = context.resources.getColor(R.color.color_00B0FF)
//        }
//    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_AEEA00)
        }
    }

//    private val mPaint3 by lazy {
//        Paint().apply {
//            isAntiAlias = true
//            isDither = true
//            //strokeCap = Paint.Cap.ROUND
//            //style = Paint.Style.STROKE
//            style = Paint.Style.FILL
//            color = context.resources.getColor(R.color.color_BF360C)
//        }
//    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

//            val mStrokeWidth = (cx / 20).coerceAtLeast(minStrokeWidth)
//            mPaint1.strokeWidth = mStrokeWidth
//            mPaint3.strokeWidth = mStrokeWidth
//
//            drawCircle(0F, 0F, cx * 3 / 7, mPaint)
//
//            for (i in 1..PIECES) {
//                drawCircle(
//                    cx * 10 / 21 * Math.cos(Math.toRadians(i * 360.0 / 6)).toFloat(),
//                    cx * 10 / 21 * Math.sin(Math.toRadians(i * 360.0 / 6)).toFloat(),
//                    cx / 10,
//                    mPaint0
//                )
//            }
//
//            drawCircle(0F, 0F, cx * 6 / 7, mPaint1)
//
//            for (i in 1..PIECES) {
//                drawCircle(
//                    cx * 18 / 21 * Math.cos(Math.toRadians(i * 360.0 / 6 + 30.0)).toFloat(),
//                    cx * 18 / 21 * Math.sin(Math.toRadians(i * 360.0 / 6 + 30.0)).toFloat(),
//                    cx / 10,
//                    mPaint2
//                )
//            }

            val baseAngel = 360.0 / PIECES
            val offsetAngel = baseAngel / 2
            mPath.apply {
                for (i in 1..PIECES) {
                    if (i == 1) {
                        moveTo(cx * 10 / 21 * Math.cos(Math.toRadians(i * baseAngel)).toFloat(),
                            cx * 10 / 21 * Math.sin(Math.toRadians(i * baseAngel)).toFloat())
                        lineTo(cx * 20 / 21 * Math.cos(Math.toRadians(i * baseAngel + offsetAngel)).toFloat(),
                            cx * 20 / 21 * Math.sin(Math.toRadians(i * baseAngel + offsetAngel)).toFloat())
                    } else {
                        lineTo(cx * 10 / 21 * Math.cos(Math.toRadians(i * baseAngel)).toFloat(),
                            cx * 10 / 21 * Math.sin(Math.toRadians(i * baseAngel)).toFloat())
                        lineTo(cx * 20 / 21 * Math.cos(Math.toRadians(i * baseAngel + offsetAngel)).toFloat(),
                            cx * 20 / 21 * Math.sin(Math.toRadians(i * baseAngel + offsetAngel)).toFloat())
                    }
                }
                close()
            }

            drawPath(mPath, mPaint2)

            restore()
        }
    }

    companion object {
        const val PIECES = 6
    }
}